﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AKBGame.Util
{
    public class GZipStreamHelper
    {
        public static string UncompressString(string base64String)
        {
            byte[] compressed = System.Convert.FromBase64String(base64String);

            string str;
            MemoryStream memoryStream = new MemoryStream(compressed);
            try
            {
                str = UncompressString(compressed, new GZipStream(memoryStream, CompressionMode.Decompress));
            }
            finally
            {
                if (memoryStream != null)
                {
                    memoryStream.Dispose();
                }
            }
            return str;
        }

        private static string UncompressString(byte[] compressed, Stream decompressor)
        {
            string end;
            byte[] numArray = new byte[1024];
            Encoding uTF8 = Encoding.UTF8;
            MemoryStream memoryStream = new MemoryStream();
            try
            {
                Stream stream = decompressor;
                try
                {
                    while (true)
                    {
                        int num = decompressor.Read(numArray, 0, (int)numArray.Length);
                        int num1 = num;
                        if (num == 0)
                        {
                            break;
                        }
                        memoryStream.Write(numArray, 0, num1);
                    }
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }
                }
                memoryStream.Seek((long)0, 0);
                end = (new StreamReader(memoryStream, uTF8)).ReadToEnd();
            }
            finally
            {
                if (memoryStream != null)
                {
                    memoryStream.Dispose();
                }
            }
            return end;
        }
    }
}
